<template>
	<view class="container">
		
		<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
			<image :src="headimgUrl" class="headimg" mode="widthFix"></image>
			<view class="edit-text">编辑</view>
		</button> 
		
		<view class="input-c">
			<view class="s-title">昵称</view>
			<!-- {{name}} -->
			<input type="nickname" @blur="inputChange" class="input" v-model="name" placeholder="请输入昵称"/>
		</view>
		<!-- <input type="nickname" class="weui-input" placeholder="请输入昵称"/> -->
		
		<view class="submit-btn" @tap="submit">确认更新</view>
		
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex"
	
	export default {
		name: "center",
		components: {},
		data() {
			return {
				headimgUrl: '',
				name: ''
			}
		},
		computed: {
			...mapGetters(["userInfo"]),
		},
		onLoad() {
			this.$store.dispatch("getUserInfo").then(res => {
				this.headimgUrl = this.userInfo.headimg
				if (this.userInfo.name != '微信用户') {
					this.name = this.userInfo.name
				}
			})
		},
		onShow() {
		},
		methods: {
			inputChange (res) {
				this.name = res.detail.value
				// console.log('input change =====>', res)
			},
			onChooseAvatar (res) {
				let tempUrl = res.detail.avatarUrl
				this.$upload.upload(tempUrl).then(url => {
					this.headimgUrl = url
				})
				
				// this.headimgUrl = res.detail.avatarUrl				
				// console.log(res)
			},
			submit () {
				if (!this.headimgUrl) {
					uni.showToast({
						title: '请选择头像~',
						icon: 'none'
					})
					
					return false
				}
				
				if (!this.name) {
					uni.showToast({
						title: '请填写昵称~',
						icon: 'none'
					})
					
					return false
				}
				
				this.$http({
					url: '/user',
					method: 'PUT',
					data: {
						name: this.name,
						headimg: this.headimgUrl,
					}
				}).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: '更新成功，跳转中~',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 1300)
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: white;
	}
	
	.headimg {
		width: 200rpx;
		height: 200rpx;
		border-radius: 10rpx;
		display: block;
		// margin: 100rpx auto;
	}
	
	.name {
		
	}
	
	.avatar-wrapper {
		// border: 1px solid red;
		width: 200rpx;
		height: 200rpx;
		padding: 0rpx;
		margin: 100rpx auto;
		.edit-text {
			background: rgba(0, 0, 0, 0.3);
			color: white;
			text-align: center;
			position: absolute;
			bottom: 0rpx;
			width: 100%;
			font-size: 20rpx;
		}
	}
	
	button {
		background: transparent;
		&:after {
			display: none;
		}
		&:before {
			display: none;
		}
	}
	
	.input-c {
		display: flex;
		align-items: center;
		border: 1rpx solid #f1f1f1;
		padding: 40rpx 30rpx;
		box-sizing: border-box;
		
		.s-title {
			width: 170rpx;
			color: #333;
			margin-left: 30rpx;
		} 
	}
	
	.submit-btn {
		// position: fixed;
		bottom: 100rpx;
		margin: 0 auto;
		margin-bottom: 10rpx;
		margin-top: 300rpx;
		
		width: 600rpx;
		height: 96rpx;
		line-height: 96rpx;
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
		color: black;
		background-color: #FFC300;
		// box-shadow: 0 0 20rpx rgba(255, 220, 0, 0.6);
		border-radius: 30rpx;
	}
	
</style>
